package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialBreakageWarehouseEntity;
import city.spring.modules.material.entity.MaterialBreakageWarehouseMaterialExtEntity;
import city.spring.modules.material.entity.MaterialQuantityEntity;
import city.spring.modules.material.repository.MaterialBreakageWarehouseMaterialExtRepository;
import city.spring.modules.material.repository.MaterialBreakageWarehouseRepository;
import city.spring.modules.material.service.MaterialBreakageWarehouseService;
import city.spring.modules.material.service.MaterialQuantityService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：部门报损
 *
 * @author 谢金婷
 * @date 2021-01-18 11:07:27
 */
@CacheConfig(cacheNames = {MaterialBreakageWarehouseServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MaterialBreakageWarehouseServiceImpl extends ServiceImpl<MaterialBreakageWarehouseRepository, MaterialBreakageWarehouseEntity> implements MaterialBreakageWarehouseService {

    private final UserService userService;
    private final MaterialBreakageWarehouseMaterialExtRepository materialBreakageWarehouseMaterialExtRepository;
    private final MaterialQuantityService materialQuantityService;
    private final MaterialBreakageWarehouseRepository materialBreakageWarehouseRepository;

    public MaterialBreakageWarehouseServiceImpl(UserService userService, MaterialBreakageWarehouseMaterialExtRepository materialBreakageWarehouseMaterialExtRepository, MaterialQuantityService materialQuantityService, MaterialBreakageWarehouseRepository materialBreakageWarehouseRepository) {
        this.userService = userService;
        this.materialBreakageWarehouseMaterialExtRepository = materialBreakageWarehouseMaterialExtRepository;
        this.materialQuantityService = materialQuantityService;
        this.materialBreakageWarehouseRepository = materialBreakageWarehouseRepository;
    }

    @Override
    public void saveMaterialBreakageWarehouse(MaterialBreakageWarehouseEntity entity) {
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setReporter(userEntity.getId());
        entity.setStatus("待审核");
        entity.setConfirm("否");
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMaterialBreakageWarehouse(MaterialBreakageWarehouseEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMaterialBreakageWarehouse(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMaterialBreakageWarehouse(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MaterialBreakageWarehouseEntity> listPage(Page<? extends MaterialBreakageWarehouseEntity> page, Wrapper<? extends MaterialBreakageWarehouseEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public void confirmWarehouse(String id,MaterialBreakageWarehouseEntity entity) {
        //获取该报损入库的物资
        List<MaterialBreakageWarehouseMaterialExtEntity> warehousingMaterials = materialBreakageWarehouseMaterialExtRepository.getWarehousingMaterials(entity.getId());
        // 更新库存
        for (MaterialBreakageWarehouseMaterialExtEntity warehousingMaterial : warehousingMaterials){
            MaterialQuantityEntity materialQuantityEntity = materialQuantityService.getByMaterialIdAndStorehouseId(warehousingMaterial.getMaterialId(), warehousingMaterial.getStorehouseId());
            BigDecimal warehousingquantity = materialQuantityEntity.getWarehousingquantity() == null ? BigDecimal.ZERO : materialQuantityEntity.getWarehousingquantity();
            BigDecimal unitprice = materialQuantityEntity.getUnitprice() == null ? BigDecimal.ZERO : materialQuantityEntity.getUnitprice();
            BigDecimal quantity = materialQuantityEntity.getQuantity() == null ? BigDecimal.ZERO : materialQuantityEntity.getQuantity();
            materialQuantityEntity.setWarehousingquantity(warehousingquantity.add(warehousingMaterial.getBreakageQuantity()));
            materialQuantityEntity.setQuantity(quantity.add(warehousingMaterial.getBreakageQuantity()));
            materialQuantityEntity.setTotal(unitprice.multiply(materialQuantityEntity.getQuantity()));
            materialQuantityService.lambdaUpdate()
                    .set(MaterialQuantityEntity::getWarehousingquantity,materialQuantityEntity.getWarehousingquantity())
                    .set(MaterialQuantityEntity::getQuantity,materialQuantityEntity.getQuantity())
                    .set(MaterialQuantityEntity::getTotal,materialQuantityEntity.getTotal())
                    .eq(MaterialQuantityEntity::getId,materialQuantityEntity.getId())
                    .update();
        }
        entity.setConfirm("是");
        materialBreakageWarehouseRepository.confirmWarehouse(id,entity);
    }
}